﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DCH.Areas.Admin.Models;
using DCH.Models;

namespace DCH.Areas.Admin.Controllers
{
    [Authorize]
    public class TournamentController : Controller
    {
        private DbConnection db = new DbConnection();
        private String ScoreCard2 = string.Empty;
        private String ScoreCard1 = "<table><tr><td>Score Card 1</td></tr></table>";
        //
        // GET: /Admin/Tournament/

        public ActionResult Index()
        {
            return View(db.Tournament.ToList());
        }

        //
        // GET: /Admin/Tournament/Details/5

        public ActionResult Details(int id = 0)
        {
            Tournament tournament = db.Tournament.Find(id);
            if (tournament == null)
            {
                return HttpNotFound();
            }
            return View(tournament);
        }

        //
        // GET: /Admin/Tournament/Create

        public ActionResult Create()
        {
            var tournament = new Tournament() { team1_score_card = ScoreCard1, team2_score_card = ScoreCard2  };
            tournament.Date = DateTime.Now;
            return View(tournament);
        }

        //
        // POST: /Admin/Tournament/Create

        [HttpPost]
        [ValidateInput(false)] 
        public ActionResult Create(Tournament tournament)
        {
            if (ModelState.IsValid)
            {
                db.Tournament.Add(tournament);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tournament);
        }

        //
        // GET: /Admin/Tournament/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Tournament tournament = db.Tournament.Find(id);
            if (tournament == null)
            {
                return HttpNotFound();
            }
            return View(tournament);
        }

        //
        // POST: /Admin/Tournament/Edit/5

        [HttpPost]
        [ValidateInput(false)] 
        public ActionResult Edit(Tournament tournament)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tournament).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tournament);
        }

        //
        // GET: /Admin/Tournament/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Tournament tournament = db.Tournament.Find(id);
            if (tournament == null)
            {
                return HttpNotFound();
            }
            return View(tournament);
        }

        //
        // POST: /Admin/Tournament/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Tournament tournament = db.Tournament.Find(id);
            db.Tournament.Remove(tournament);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}